{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9cf639fa-dffe-4287-9c10-39b688435b39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成嵌入向量数量: 5\n",
      "每个向量的维度: 1536\n",
      "[0.20767687261104584, 2.144308090209961, 1.4478895664215088, 0.0217420794069767, 2.440538167953491]\n"
     ]
    }
   ],
   "source": [
    "# Embed_documents\n",
    "# 首先安装所需库\n",
    "# pip install dashscope langchain-openai\n",
    "\n",
    "from langchain.embeddings import DashScopeEmbeddings\n",
    "import os\n",
    "\n",
    "# 设置阿里云API Key（从环境变量读取或直接设置）\n",
    "os.environ[\"DASHSCOPE_API_KEY\"] = \"sk-4e88cf4db3e14894bafaff606d296610\"  # 替换为你的实际Key\n",
    "\n",
    "# 初始化嵌入模型\n",
    "e_model = DashScopeEmbeddings(\n",
    "    model=\"text-embedding-v1\",  # 使用text-embedding-v1模型\n",
    ")\n",
    "\n",
    "# 生成嵌入向量\n",
    "embeddings = e_model.embed_documents([\n",
    "    \"你好\",\n",
    "    \"你好啊\", \n",
    "    \"你叫什么名字?\",\n",
    "    \"我叫王大锤\",\n",
    "    \"很高兴认识你大锤\",\n",
    "])\n",
    "\n",
    "print(f\"生成嵌入向量数量: {len(embeddings)}\")\n",
    "print(f\"每个向量的维度: {len(embeddings[0]) if embeddings else 0}\")\n",
    "\n",
    "# embed_query\n",
    "embedded_query = e_model.embed_query(\"这段对话中提到了什么名字?\")\n",
    "print(embedded_query[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "24ba8b9f-1f79-45e9-98e3-4ca0b195fb61",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Created a chunk of size 610, which is longer than the specified 600\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['text-embedding-ada-0024250f053-4b1e-5c34-927d-a7857749217f', 'text-embedding-ada-0029286d74c-b3fc-56ff-8b08-9071a193f724', 'text-embedding-ada-002b0c54c27-a009-50b4-9ccc-661d5478b195', 'text-embedding-ada-002c63ea318-3b5d-533b-960b-46434f8b3c22', 'text-embedding-ada-002e94acbbe-7d17-5331-8310-4e37bdc56d31', 'text-embedding-ada-002f05b40fb-a095-546e-9c5d-49e069720828']\n",
      "[Document(metadata={'source': 'kecheng/letter.txt'}, page_content='[Generated with ChatGPT]\\n\\nConfidential Document - For Internal Use Only\\n\\nDate: July 1, 2023\\n\\nSubject: Updates and Discussions on Various Topics\\n\\nDear Team,\\n\\nI hope this email finds you well. In this document, I would like to provide you with some important updates and discuss various topics that require our attention. Please treat the information contained herein as highly confidential.'), Document(metadata={'source': 'kecheng/letter.txt'}, page_content=\"Security and Privacy Measures\\nAs part of our ongoing commitment to ensure the security and privacy of our customers' data, we have implemented robust measures across all our systems. We would like to commend John Doe (email: john.doe@example.com) from the IT department for his diligent work in enhancing our network security. Moving forward, we kindly remind everyone to strictly adhere to our data protection policies and guidelines. Additionally, if you come across any potential security risks or incidents, please report them immediately to our dedicated team at security@example.com.\"), Document(metadata={'source': 'kecheng/letter.txt'}, page_content='HR Updates and Employee Benefits\\nRecently, we welcomed several new team members who have made significant contributions to their respective departments. I would like to recognize Jane Smith (SSN: 049-45-5928) for her outstanding performance in customer service. Jane has consistently received positive feedback from our clients. Furthermore, please remember that the open enrollment period for our employee benefits program is fast approaching. Should you have any questions or require assistance, please contact our HR representative, Michael Johnson (phone: 418-492-3850, email: michael.johnson@example.com).'), Document(metadata={'source': 'kecheng/letter.txt'}, page_content='Marketing Initiatives and Campaigns\\nOur marketing team has been actively working on developing new strategies to increase brand awareness and drive customer engagement. We would like to thank Sarah Thompson (phone: 415-555-1234) for her exceptional efforts in managing our social media platforms. Sarah has successfully increased our follower base by 20% in the past month alone. Moreover, please mark your calendars for the upcoming product launch event on July 15th. We encourage all team members to attend and support this exciting milestone for our company.'), Document(metadata={'source': 'kecheng/letter.txt'}, page_content=\"Research and Development Projects\\nIn our pursuit of innovation, our research and development department has been working tirelessly on various projects. I would like to acknowledge the exceptional work of David Rodriguez (email: david.rodriguez@example.com) in his role as project lead. David's contributions to the development of our cutting-edge technology have been instrumental. Furthermore, we would like to remind everyone to share their ideas and suggestions for potential new projects during our monthly R&D brainstorming session, scheduled for July 10th.\"), Document(metadata={'source': 'kecheng/letter.txt'}, page_content=\"Please treat the information in this document with utmost confidentiality and ensure that it is not shared with unauthorized individuals. If you have any questions or concerns regarding the topics discussed, please do not hesitate to reach out to me directly.\\n\\nThank you for your attention, and let's continue to work together to achieve our goals.\\n\\nBest regards,\\n\\nJason Fan\\nCofounder & CEO\\nPsychic\\njason@psychic.dev\")]\n",
      "756 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "# 嵌入向量缓存\n",
    "from langchain.embeddings import CacheBackedEmbeddings\n",
    "from langchain.storage import  LocalFileStore\n",
    "from langchain.document_loaders import TextLoader\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "# from langchain_openai import OpenAIEmbeddings\n",
    "from langchain.embeddings import DashScopeEmbeddings\n",
    "from langchain.vectorstores import  FAISS\n",
    "\n",
    "# u_embeddings = OpenAIEmbeddings()\n",
    "u_embeddings = DashScopeEmbeddings(\n",
    "    model=\"text-embedding-v1\",  # 使用text-embedding-v1模型\n",
    ")\n",
    "fs = LocalFileStore(\"kecheng/cache/\")\n",
    "cached_embeddings = CacheBackedEmbeddings.from_bytes_store(\n",
    "    u_embeddings,\n",
    "    fs,\n",
    "    namespace=u_embeddings.model,\n",
    ")\n",
    "print(list(fs.yield_keys()))\n",
    "\n",
    "#加载文档，切分文档，将切分文档向量化病存储在缓存中\n",
    "raw_documents = TextLoader(\"kecheng/letter.txt\").load()\n",
    "text_splitter = CharacterTextSplitter(chunk_size=600,chunk_overlap=0)\n",
    "documents = text_splitter.split_documents(raw_documents)\n",
    "print(documents)\n",
    "\n",
    "%timeit -r  1 -n 1 db= FAISS.from_documents(documents,cached_embeddings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "88839137-9c97-4f93-badc-bc0a8052bc6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['text-embedding-ada-0024250f053-4b1e-5c34-927d-a7857749217f',\n",
       " 'text-embedding-ada-0029286d74c-b3fc-56ff-8b08-9071a193f724',\n",
       " 'text-embedding-ada-002b0c54c27-a009-50b4-9ccc-661d5478b195',\n",
       " 'text-embedding-ada-002c63ea318-3b5d-533b-960b-46434f8b3c22',\n",
       " 'text-embedding-ada-002e94acbbe-7d17-5331-8310-4e37bdc56d31',\n",
       " 'text-embedding-ada-002f05b40fb-a095-546e-9c5d-49e069720828',\n",
       " 'text-embedding-v14250f053-4b1e-5c34-927d-a7857749217f',\n",
       " 'text-embedding-v19286d74c-b3fc-56ff-8b08-9071a193f724',\n",
       " 'text-embedding-v1b0c54c27-a009-50b4-9ccc-661d5478b195',\n",
       " 'text-embedding-v1c63ea318-3b5d-533b-960b-46434f8b3c22',\n",
       " 'text-embedding-v1e94acbbe-7d17-5331-8310-4e37bdc56d31',\n",
       " 'text-embedding-v1f05b40fb-a095-546e-9c5d-49e069720828']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看缓存中的键\n",
    "list(fs.yield_keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ac2811b-33de-41cf-b6e0-b60189066017",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rag_learn",
   "language": "python",
   "name": "rag_learn"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
